package com.example.web.office_specifications.entity;

import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class MakeSpecificationsTree {

    public static List<OfficeSpecifications> makeTree(List<OfficeSpecifications> treeList, Long pid){
        //存放组装的树数据
        List<OfficeSpecifications> list = new ArrayList<>();
        //组装树
        Optional.ofNullable(treeList).orElse(new ArrayList<>())
                .stream()
                .filter(item -> item != null && item.getParentId().equals(pid))
                .forEach(item -> {
                    OfficeSpecifications tree = new OfficeSpecifications();
                    BeanUtils.copyProperties(item, tree);
                    //查找下级：递归调用；自己调用自己
                    List<OfficeSpecifications> children = makeTree(treeList, item.getId());
                    tree.setChildren(children);
                    list.add(tree);
                });
        return list;
    }
}
